package com.coder.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.json.JSONObject;

/**
 * 系统统一返回信息
 * @author Yeltsin
 *
 * @param <T>
 */
public class WebResult<T> extends HashMap<Object, Object> implements Serializable {
	
	public enum Code{
		/** 成功  */
		SUCCESS(0,"成功"),
		/** 系统异常 */
		ERROR(100,"系统异常"),
		/** 添加失败  */
		ADD_FAILED(1001,"添加失败"),
		/** 删除失败 */
		DELTED_FAILED(1002,"删除失败"),
		/** App Id错误 */
		APPID_ERROR(1003,"App Id错误"),
		/** 签名错误 */
		SIGN_ERROR(1004,"签名错误"),
		/** 接口不存在 */
		INTERFACE_NO_HAVE_ERROR(1005,"接口不存在"),
		/** 接口已停用 */
		INTERFACE_STOPED_ERROR(1006,"接口已停用"),
		/** token异常 */
		TOKEN_ERROR(1007,"token异常"),
		/** 用户名必输 */
		USER_NAME_MUST_ERROR(1008,"用户名必输"),
		/** 未绑定用户 */
		USER_NO_BIND_ERROR(1009,"未绑定用户"),
		/** 第三方账号类型错误 */
		THIRD_TYPE_ERROR(1010,"第三方账号类型错误"),
		/** 链接不可重复访问 */
		LINK_VISIT_ERROR(1011,"链接不可重复访问"),
		/** uid不能为空 */
		UID_IS_EMPTY_ERROR(1012,"uid不能为空"),
		/** 手机号不能为空 */
		PHONE_IS_EMPTY(1013,"手机号不能为空"),
		/** 手机号不能为空 */
		PHONE_HAS_REGISTER(1014,"手机号已被注册"),
		/** 手机号不能为空 */
		PHONE_HAS_NOT_REGISTER(1015,"手机号未注册过"),
		/** 验证码错误 */
		CAPTCHA_WRONG(1016,"验证码错误"),
		/** 验证码错误 */
		OUT_OF_DATE(1017,"商户使用已到期，请续期"),
		/** 时间戳错误 */
		TIMESTAMP_ERROR(1018,"时间戳错误"),
		/** 随机数错误 */
		NONSTR_ERROR(1019,"随机数错误"),
		/** 设备号错误 */
		DEVICEID_ERROR(1020,"设备号错误"),
		/** 设请求非法 */
		REQUEST_ERROR(1021,"设请求非法");
		public int code;
		
		public String message;
		
		private Code(int code,String message){
			this.code = code;
			this.message = message;
		}
		
		public int getCode(){
			return this.code;
		}
		
		public String getMessage(){
			return message;
		}
		
	}
	
	private static final long serialVersionUID = -6580070774548033814L;

	/**
	 * 错误信息
	 */
	private static final String KEY_MSG = "msg";
	
	/**
	 * 数据返回代码
	 * 	0：成功
	 * 	100：系统异常
	 *  101：登录失败
	 */
	private static final String KEY_CODE = "code";
		
	public WebResult(){
		this(Code.SUCCESS);
	}
	
	public WebResult(Code code){
		this.put(KEY_CODE, code.getCode());
		this.put(KEY_MSG, code.getMessage());
	}
	
	public WebResult(Map<String,T> result){
		this(Code.SUCCESS);
		this.putAll(result);
	}
	
	public WebResult(Code code,Map<String,T> result){
		this(code);
		this.putAll(result);
	}
	
	public static WebResult<String> getErrorResult(String msg){
		WebResult<String> result = new WebResult<String>();
		result.setCode(100);
		result.setMsg(msg);
		return result;
	}
	
	public static <T> WebResult<T> getSuccessResult(String key,T t){
		WebResult<T> result = new WebResult<T>();
		result.put(key, t);
		return result;
	}
	
	public void putResult(String key,T t){
		this.put(key, t);
	}
	
	public static <T> WebResult<T> getSuccessResult(){
		return new WebResult<T>();
	}
		
	public static <T> WebResult<T> getErrorResult(Code code){
		return new WebResult<T>(code);
	}
	
	/**
	 * 是否成功
	 * @return
	 */
	public boolean isSuccess(){
		return MapUtils.getIntValue(this, KEY_CODE) == Code.SUCCESS.getCode();
	}

	public String getMsg() {
		return MapUtils.getString(this, KEY_MSG);
	}

	public void setMsg(String msg) {
		this.put(KEY_MSG, msg);
	}

	public int getCode() {
		return MapUtils.getIntValue(this, KEY_CODE);
	}

	public void setCode(int code) {
		this.put(KEY_CODE, code);
	}

	@Override
	public String toString() {
		return new JSONObject(this).toString();
	}

}
